﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../global/head.h"
#include "id_generator.h"
#include "common_impl.h"
#include "mem_alloc.h"
#include "misc.h"
#include "sha.h"
#include "md5.h"

////////////////////////////////////////////////////////////////////////////////////////////
// 

namespace xos_common
{

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    common_impl::common_impl()
    {
        g_pSysData->m_pCommon = this;
    }

    common_impl::~common_impl()
    {
        if( g_pSysData )
        {
            g_pSysData->term();
            delete g_pSysData;
            g_pSysData = 0;
        }
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 

    // 
    // create objects
    // 
    int common_impl::create( enumObjType enumType, void ** ppv )
    {
        int nRet = 0;

        *ppv = 0;

        switch( enumType )
        {
		case OBJ_ID_GENERATOR:
			{
                id_generator * p = id_generator::get_item_from_pool( true );
				p->init();
				*ppv = p;
			}
			break;
        case OBJ_ALLOC:
            {
				mem_alloc * p = mem_alloc::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;
        case OBJ_MD5:
            {
                md5 * p = md5::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;
        case OBJ_MISC:
            {
                misc * p = misc::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;
        case OBJ_SHA:
            {
                sha * p = sha::get_item_from_pool( true );
                p->init();
                *ppv = p;
            }
            break;
        default:
            {
                nRet = 1;
            }
            break;
        }

        if( !( *ppv ) )
        {
            nRet = 1;
        }

        return nRet;
    }

    /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // 
    // for com_object_no_ref to use.
    // 

    int common_impl::put_back_to_pool( T * pT, bool bLock )
    {
        int ret = 0;
        delete this;
        return ret;
    }

    int common_impl::init()
    {
        int ret = 0;
        return ret;
    }

    int common_impl::term()
    {
        int ret = 0;
        return ret;
    }

} // xos_common
